<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if(!function_exists('file_getExt'))
{
	function file_getExt($file)
	{
		$dot = strrpos($file, '.') + 1;

		return substr($file, $dot);
	}
}

if(!function_exists('file_stripExt'))
{
	function file_stripExt($file)
	{
		return preg_replace('#\.[^.]*$#', '', $file);
	}
}

if(!function_exists('file_makeSafe'))
{
	function file_makeSafe($file)
	{
		// Remove any trailing dots, as those aren't ever valid file names.
		$file = rtrim($file, '.');

		$regex = array('#(\.){2,}#', '#[^A-Za-z0-9\.\_\- ]#', '#^\.#');

		return trim(preg_replace($regex, '', $file));
	}
}

if(!function_exists('file_copy'))
{
	function file_copy($src, $dest, $path = null, $use_streams = false)
	{
		// Prepend a base path if it exists
		if ($path)
		{
			$src = path_lean($path . '/' . $src);
			$dest = path_clean($path . '/' . $dest);
		}

		// Check src path
		if (!is_readable($src))
		{
			return false;
		}

		if ($use_streams)
		{
		}
		else
		{
			if (false)
			{
			}
			else
			{
				if (!@ copy($src, $dest))
				{
					return false;
				}

				$ret = true;
			}

			return $ret;
		}
	}
}

if(!function_exists('file_delete'))
{
	function file_delete($file)
	{
		if (is_array($file))
		{
			$files = $file;
		}
		else
		{
			$files[] = $file;
		}

		// Do NOT use ftp if it is not enabled
		if (false)
		{
		}

		foreach ($files as $file)
		{
			$file = path_clean($file);

			if (!is_file($file))
			{
				continue;
			}

			// Try making the file writable first. If it's read-only, it can't be deleted
			// on Windows, even if the parent folder is writable
			@chmod($file, 0777);

			// In case of restricted permissions we zap it one way or the other
			// as long as the owner is either the webserver or the ftp
			if (@unlink($file))
			{
				// Do nothing
			}
			elseif (false)
			{
			}
			else
			{
				$filename = basename($file);
				return false;
			}
		}

		return true;
	}
}

if(!function_exists('file_upload'))
{
	function file_upload($src, $dest, $use_streams = false)
	{
		// Ensure that the path is valid and clean
		$dest = path_clean($dest);

		// Create the destination directory if it does not exist
		$baseDir = dirname($dest);

		if (!file_exists($baseDir))
		{
			folder_create($baseDir);
		}

		if ($use_streams)
		{
		}
		else
		{
			$ret = false;

			if (false)
			{
				// Connect the FTP client
			}
			else
			{
				if (is_writeable($baseDir) && move_uploaded_file($src, $dest))
				{
					// Short circuit to prevent file permission errors
					if (path_setPermissions($dest))
					{
						$ret = true;
					}
					else
					{
					}
				}
				else
				{
				}
			}

			return $ret;
		}
	}
}

if(!function_exists('file_isFile'))
{
	function file_isFile($file)
	{
		return is_file(path_clean($file));
	}
}

if(!function_exists('file_getName'))
{
	function file_getName($file)
	{
		// Convert back slashes to forward slashes
		$file = str_replace('\\', '/', $file);
		$slash = strrpos($file, '/');

		if ($slash !== false)
		{
			return substr($file, $slash + 1);
		}
		else
		{
			return $file;
		}
	}
}
